package com.sinoglobal.ningxia.service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.MessageVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.thread.ItktOtherAsyncTask;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;

/**
 * @author yrq
 * @createdate 2014-07-24 下午15:25:43
 * @Description: TODO 我的消息 推送消息
 */
public class PushService extends Service {
	private Timer mTimer;
	private TimerTask mTimerTask;
	private static long delay = 1000;
	private static long period = 1000 * 3600;
	// private static long period = 1000 * 30;
	private int id = 0;
	private String imei="";
	private String isMessagePush = "1"; // 消息推送
	private String isActivityPush = "1"; // 活动推送

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		StartTimer();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mTimer != null) {
			mTimer.cancel();
		}

	}

	private void StartTimer() {
		if (mTimer == null) {
			mTimerTask = new TimerTask() {
				@Override
				public void run() {
					mHandler.sendEmptyMessage(1);
				}
			};
			mTimer = new Timer();
			mTimer.schedule(mTimerTask, delay, period);
		}
	}

	Handler mHandler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if (msg.what == 1) {
				getTuiSong();// TODO推送消息
			} else if (msg.what == 2) {
			}
		}
	};

	/**
	 * 推送消息
	 */
	public void getTuiSong() {
		try {
			imei = FlyApplication.getUniquely(getApplicationContext()); // 获取手机唯一标识码
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (SharedPreferenceUtil.getBoolean(FlyApplication.context, "isMessagePush")) {
			isMessagePush = "1";
		} else {
			isMessagePush = "2";
		}
		if (SharedPreferenceUtil.getBoolean(FlyApplication.context, "isActivityPush")) {
			isActivityPush = "1";
		} else {
			isActivityPush = "2";
		}
		/**
		 * 若 消息推送和 活动推送都不推送则不访问网络 否则会访问接口
		 */
		if (isMessagePush.equals("2") && isActivityPush.equals("2")) {

		} else {
			new ItktOtherAsyncTask<Void, Void, MessageVo>(false) {

				@Override
				public MessageVo before(Void... params) throws Exception {
					return RemoteImpl.getInstance().getMessageInfo(imei, isActivityPush, isMessagePush);
					// return JSON.parseObject(TestJson.getMessageVo(),
					// MessageVo.class);
				}

				@Override
				public void after(MessageVo result) {
					// ShowNotification(result);
					/**
					 * 将消息保存到共享参数里面
					 * 
					 * 注意共享参数里面的我的消息列表的内容的添加
					 */
					MessageVo oldRresultMessageVo = new MessageVo();
					// 获取 请求网络之前的消息内容
					String messageString = SharedPreferenceUtil.getString(getApplicationContext(), "messageVo");
					if (messageString != null && messageString.length() > 0) {
						// 获取请求网络之前的消息列表
						oldRresultMessageVo = JSON.parseObject(messageString, MessageVo.class);
					}
					if (result.getList() != null && result.getList().size() != 0) {
						// 将请求网络获得的消息 添加到请求网络之前的消息列表中
						oldRresultMessageVo.getList().addAll(0, result.getList());
					}
					// 保存最新的消息列表
					SharedPreferenceUtil.saveString(getApplicationContext(), "messageVo", JSON.toJSONString(oldRresultMessageVo));

				}

				// private void ShowNotification(MessageVo result) {
				// NotificationManager nfm = (NotificationManager)
				// getSystemService(NOTIFICATION_SERVICE);
				// Notification nfn = new Notification(R.drawable.ic_launcher,
				// result.getList().get(0).getTitle(),
				// System.currentTimeMillis() +
				// 20);
				// Intent intent = null;
				// intent = new Intent(getApplicationContext(),
				// MyMessageActivity.class);
				// // intent.putExtra("url", result.getUrl());
				// PendingIntent PendingIntentintent =
				// PendingIntent.getActivity(getApplicationContext(), 0, intent,
				// 0);
				// nfn.setLatestEventInfo(getApplicationContext(),
				// result.getList().get(0).getTitle(),
				// result.getList().get(0).getContent(), PendingIntentintent);
				// nfn.flags = Notification.FLAG_AUTO_CANCEL;
				// nfm.notify(id, nfn);
				// }

				@Override
				public void exception() {

				}
			}.execute();
		}
	}
}